package org.ibase4j.assistant;

import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class AboutSixtyJob implements Job {
	private final int FiveMinutes = 1;
	private final int FifteenMinutes = 2;
	private final int ThirtyMinutes = 3;
	private final int SixtyMinutes = 4;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		String jobName = context.getJobDetail().getKey().getName();
		String groupName = context.getJobDetail().getKey().getGroup();
		insertSixty(jobName, groupName);
	}

	private void insertSixty(String jobName, String groupName) {
		Connection conn = null; // 数据库连接
		PreparedStatement pstmt = null; // 数据库操作
		String sql = "";
		Map<String, Object> sixtyMinutesMap = null;
		try {
			if ("sys_zhong_five".equals(jobName) && "group1".equals(groupName) || "sys_zhong_fifteen".equals(jobName) && "group4".equals(groupName) || "sys_zhong_thirty".equals(jobName) && "group7".equals(groupName) || "sys_zhong_sixty".equals(jobName) && "group10".equals(groupName)) {
				sql = "insert into " + jobName + "(daydate,openv,high,low,close) values(?,?,?,?,?) ";
				sixtyMinutesMap = CrawlingZhongZhengUtil.collectRealTime();
			} else if ("sys_zhong_five".equals(jobName) && "group2".equals(groupName) || "sys_zhong_fifteen".equals(jobName) && "group5".equals(groupName) || "sys_zhong_thirty".equals(jobName) && "group8".equals(groupName) || "sys_zhong_sixty".equals(jobName) && "group11".equals(groupName)) {
				sql = "update " + jobName + " set daydate =?,openv =?,high =?,close =?,low =?  where id=(select id from (select max(id) id from " + jobName + ") a)";
				sixtyMinutesMap = CrawlingZhongZhengUtil.collectRealTime();
			} else if ("sys_zhong_five".equals(jobName) && "group3".equals(groupName) || "sys_zhong_fifteen".equals(jobName) && "group6".equals(groupName) || "sys_zhong_thirty".equals(jobName) && "group9".equals(groupName) || "sys_zhong_sixty".equals(jobName) && "group12".equals(groupName)) {
				sql = "update " + jobName + " set daydate =?,openv =?,high =?,close =?,low =?  where id=(select id from (select max(id) id from " + jobName + ") a)";
				if ("group3".equals(groupName)) {
					sixtyMinutesMap = CrawlingZhongZhengUtil.collect(FiveMinutes);
				} else if ("group6".equals(groupName)) {
					sixtyMinutesMap = CrawlingZhongZhengUtil.collect(FifteenMinutes);
				} else if ("group9".equals(groupName)) {
					sixtyMinutesMap = CrawlingZhongZhengUtil.collect(ThirtyMinutes);
				} else if ("group12".equals(groupName)) {
					sixtyMinutesMap = CrawlingZhongZhengUtil.collect(SixtyMinutes);
				}
			}
			conn = JDBCUtil.getConnection();
			if (sql != null && !sixtyMinutesMap.isEmpty()) {
				pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
				Timestamp tiem = (Timestamp) sixtyMinutesMap.get("ymd");
				double openv = (double) sixtyMinutesMap.get("openv");
				double high = (double) sixtyMinutesMap.get("high");
				double low = (double) sixtyMinutesMap.get("low");
				double close = (double) sixtyMinutesMap.get("close");
				pstmt.setTimestamp(1, tiem);
				pstmt.setDouble(2, openv);
				pstmt.setDouble(3, high);
				pstmt.setDouble(4, low);
				pstmt.setDouble(5, close);
				SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
				int alls = Integer.parseInt(sdf.format(new Date()).toString().replace(":", ""));
				// 判断时间根据job名称
				judgeJobName(jobName, groupName, pstmt, alls);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.closeAll();
			if (pstmt != null) {  
	            try {  
	            	pstmt.close();  
	            } catch (SQLException e) {  
	                System.out.println(e.getMessage());  
	            }  
	        }  
			if (conn != null) {  
	            try {  
	            	conn.close();  
	            } catch (SQLException e) {  
	                System.out.println(e.getMessage());  
	            }  
	        }  
		}
	}

	private void judgeJobName(String jobName, String groupName, PreparedStatement pstmt, int alls)  {
		if ("sys_zhong_five".equals(jobName) && "group1".equals(groupName) || "sys_zhong_fifteen".equals(jobName) && "group4".equals(groupName) || "sys_zhong_thirty".equals(jobName) && "group7".equals(groupName) || "sys_zhong_sixty".equals(jobName) && "group10".equals(groupName)) {
			if (93000 <= alls && alls < 113100 || 130400 < alls && alls < 150100) {
				try {
					pstmt.executeUpdate();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					JDBCUtil.closeAll();
				}
				System.out.println("insert成功"+jobName+",group,"+groupName+",timeamp,"+alls);
			}
		} else if ("sys_zhong_five".equals(jobName) && "group2".equals(groupName) || "sys_zhong_fifteen".equals(jobName) && "group5".equals(groupName) || "sys_zhong_thirty".equals(jobName) && "group8".equals(groupName) || "sys_zhong_sixty".equals(jobName) && "group11".equals(groupName)) {
			if (93000 < alls && alls < 113600|| 125800 < alls && alls < 151100) {
				try {
					pstmt.executeUpdate();
				} catch (SQLException e) {
					e.printStackTrace();
				}finally {
					JDBCUtil.closeAll();
				}
				System.out.println("update 成功:" + jobName+",group,"+groupName+",timeamp,"+alls);
			}
		} else if ("sys_zhong_five".equals(jobName) && "group3".equals(groupName) || "sys_zhong_fifteen".equals(jobName) && "group6".equals(groupName) || "sys_zhong_thirty".equals(jobName) && "group9".equals(groupName) || "sys_zhong_sixty".equals(jobName) && "group12".equals(groupName)) {
			if (93000 < alls && alls < 1130100 || alls > 130200 && alls < 150100) {
				try {
					pstmt.executeUpdate();
				} catch (SQLException e) {
					e.printStackTrace();
				}finally {
					JDBCUtil.closeAll();
				}
				System.out.println("updatereal成功"+ jobName+",group,"+groupName+",timeamp,"+alls);
			}
		}

	}
}
